// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   InitServiceImp.java

package com.iphaeton.lms.business.init;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.business.organize.IGroupService;
import com.iphaeton.lms.business.organize.IOrganizeService;
import com.iphaeton.lms.business.role.IPrivilegeService;
import com.iphaeton.lms.business.role.IRoleService;
import com.iphaeton.lms.pojo.address.AddressSort;
import com.iphaeton.lms.pojo.course.CourseSort;
import com.iphaeton.lms.pojo.exam.QuestionHard;
import com.iphaeton.lms.pojo.exam.QuestionSort;
import com.iphaeton.lms.pojo.organize.*;
import com.iphaeton.lms.pojo.role.*;
import com.iphaeton.persistence.IPersistenceService;
import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

// Referenced classes of package com.iphaeton.lms.business.init:
//            IInitService

public class InitServiceImp extends PersistentServiceSupport
    implements IInitService
{

    public InitServiceImp()
    {
        organize = null;
        group = null;
        role = null;
        privilege = null;
        saxReader = new SAXReader();
    }

    public IOrganizeService getOrganize()
    {
        return organize;
    }

    public void setOrganize(IOrganizeService organize)
    {
        this.organize = organize;
    }

    public IGroupService getGroup()
    {
        return group;
    }

    public void setGroup(IGroupService group)
    {
        this.group = group;
    }

    public IPrivilegeService getPrivilege()
    {
        return privilege;
    }

    public void setPrivilege(IPrivilegeService privilege)
    {
        this.privilege = privilege;
    }

    public IRoleService getRole()
    {
        return role;
    }

    public void setRole(IRoleService role)
    {
        this.role = role;
    }

    public void initSystem(String realPath)
        throws BusinessException
    {
        initPrivilege(realPath + "privilege.xml");
        initRole(realPath + "role.xml");
        Organize org = initOrganize(realPath + "organize.xml");
        initUser(realPath + "user.xml", org);
        initCourseSort();
        initQuestion(realPath + "question.xml");
        initAddress(realPath + "address.xml");
    }

    private void initPrivilege(String path)
        throws BusinessException
    {
        Document document;
        try
        {
            document = saxReader.read(new File(path));
        }
        catch(DocumentException e1)
        {
            e1.printStackTrace();
            throw new BusinessException("\u89E3\u6790\u521D\u59CB\u5316\u6743\u9650xml\u6587\u4EF6\u51FA\u9519!");
        }
        Element root = document.getRootElement();
        List privilegeList = root.elements("privilege");
        for(int i = 0; i < privilegeList.size(); i++)
        {
            Privilege p = new Privilege();
            Element privilege = (Element)privilegeList.get(i);
            String privilegeName = privilege.attributeValue("name");
            String privilegeSort = privilege.attributeValue("sort");
            String privilegeComment = privilege.attributeValue("comment");
            p.setName(privilegeName);
            p.setSort(Integer.parseInt(privilegeSort));
            p.setComment(privilegeComment);
            try
            {
                p = (Privilege)hbpservice.store(p);
                hbpservice.commit();
            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u521D\u59CB\u5316\u6743\u9650\u51FA\u9519: " + p.getName());
            }
            List menuList = privilege.elements("menu");
            for(int j = 0; j < menuList.size(); j++)
            {
                Menu m = new Menu();
                Element menu = (Element)menuList.get(j);
                String menuName = menu.attributeValue("name");
                String menuAction = menu.attributeValue("function");
                String menuImage = menu.attributeValue("image");
                m.setName(menuName);
                m.setAction(menuAction);
                m.setPrivilegeId(p.getId());
                m.setImage(menuImage);
                try
                {
                    m = (Menu)hbpservice.store(m);
                    logger.info("\u521D\u59CB\u5316\u83DC\u5355: [" + m.getName() + "] \u6210\u529F!");
                }
                catch(PersistenceException e)
                {
                    e.printStackTrace();
                    throw new BusinessException("\u521D\u59CB\u5316\u83DC\u5355\u51FA\u9519: " + m.getName());
                }
            }

            logger.info("\u521D\u59CB\u5316\u6743\u9650: [" + p.getName() + "] \u6210\u529F!");
        }

    }

    private void initRole(String path)
        throws BusinessException
    {
        List nameList = null;
        List commentList = null;
        List createrList = null;
        List cnameList = null;
        try
        {
            Document document = saxReader.read(new File(path));
            nameList = document.selectNodes("/roles/role/@name");
            commentList = document.selectNodes("/roles/role/@comment");
            createrList = document.selectNodes("/roles/role/@creater");
            cnameList = document.selectNodes("/roles/role/@cname");
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u89E3\u6790role.xml\u6587\u4EF6\u9519\u8BEF:" + path);
        }
        try
        {
            for(int i = 0; i < nameList.size(); i++)
            {
                Role r = new Role();
                String name = ((Attribute)nameList.get(i)).getValue();
                r.setName(name);
                r.setComment(((Attribute)commentList.get(i)).getValue());
                r.setCreater(((Attribute)createrList.get(i)).getValue());
                r.setCname(((Attribute)cnameList.get(i)).getValue());
                hbpservice.store(r);
                logger.info("\u521D\u59CB\u5316\u89D2\u8272: [" + ((Attribute)nameList.get(i)).getValue() + "] \u6210\u529F!");
                if(name.equals("\u7CFB\u7EDF\u7BA1\u7406\u5458"))
                {
                    List list = privilege.findPrivilege(1);
                    assignPrivilege(r, list);
                } else
                if(name.equals("\u5B66\u4E60\u7BA1\u7406\u5458"))
                {
                    List list = privilege.findPrivilege(2);
                    assignPrivilege(r, list);
                } else
                if(name.equals("\u6559\u5E08"))
                {
                    List list = privilege.findPrivilege(3);
                    assignPrivilege(r, list);
                } else
                if(name.equals("\u5B66\u5458"))
                {
                    List list = privilege.findPrivilege(4);
                    assignPrivilege(r, list);
                }
                logger.info("\u5206\u914D\u6743\u9650\u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u89D2\u8272\u9519\u8BEF!");
        }
    }

    private void assignPrivilege(Role r, List list)
        throws BusinessException
    {
        for(int j = 0; j < list.size(); j++)
            try
            {
                role.addPrivilege(r, (Privilege)list.get(j));
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u5206\u914D\u6743\u9650\u9519\u8BEF!");
            }

    }

    private void initUser(String path, Organize org)
        throws BusinessException
    {
        List idList = null;
        List nameList = null;
        List passwordList = null;
        List stateList = null;
        List idcardList = null;
        Group g = null;
        try
        {
            Document document = saxReader.read(new File(path));
            idList = document.selectNodes("/users/user/@id");
            nameList = document.selectNodes("/users/user/@name");
            passwordList = document.selectNodes("/users/user/@password");
            stateList = document.selectNodes("/users/user/@state");
            idcardList = document.selectNodes("/users/user/@idcard");
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u89E3\u6790user.xml\u6587\u4EF6\u9519\u8BEF:" + path);
        }
        try
        {
            String hql = "from Group g where g.organizeId='" + org.getId() + "'";
            List manageGroup = hbpservice.query(hql);
            if(manageGroup != null && manageGroup.size() == 1)
                g = (Group)manageGroup.get(0);
            for(int i = 0; i < idList.size(); i++)
            {
                User u = new User();
                u.setUserId(((Attribute)idList.get(i)).getValue());
                u.setName(((Attribute)nameList.get(i)).getValue());
                u.setPassword(((Attribute)passwordList.get(i)).getValue());
                u.setState(((Attribute)stateList.get(i)).getValue().charAt(0));
                u.setIdcard(((Attribute)idcardList.get(i)).getValue());
                u.setSex('f');
                organize.addUser(org, u);
                logger.info("\u521D\u59CB\u5316\u7CFB\u7EDF\u7528\u6237: [" + ((Attribute)nameList.get(i)).getValue() + "] \u6210\u529F!");
                Role r = role.findSystemAdmin();
                group.addUser(g, u);
                logger.info("\u4E3A\u7BA1\u7406\u5458\u5206\u914D\u7BA1\u7406\u5BF9\u8C61\u6210\u529F!");
                role.setRole(role.findStudent(), u);
                logger.info("\u65B0\u7528\u6237\u9ED8\u8BA4\u4E3A\u5B66\u5458!");
                role.setRole(r, u);
                logger.info("\u8BBE\u7F6E [\u7CFB\u7EDF\u7BA1\u7406\u5458] \u89D2\u8272\u6210\u529F!");
                role.setRole(role.findStudyAdmin(), u);
                logger.info("\u8BBE\u7F6E [\u5B66\u4E60\u7BA1\u7406\u5458] \u89D2\u8272\u6210\u529F!");
                role.setRole(role.findTeacher(), u);
                logger.info("\u8BBE\u7F6E [\u6559\u5E08] \u89D2\u8272\u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u7528\u6237\u9519\u8BEF!");
        }
    }

    private Organize initOrganize(String path)
        throws BusinessException
    {
        List nameList = null;
        List commentList = null;
        List typeList = null;
        Organize org = new Organize();
        try
        {
            Document document = saxReader.read(new File(path));
            nameList = document.selectNodes("/organizes/organize/@name");
            commentList = document.selectNodes("/organizes/organize/@comment");
            typeList = document.selectNodes("/organizes/organize/@type");
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u89E3\u6790organize.xml\u6587\u4EF6\u9519\u8BEF:" + path);
        }
        try
        {
            for(int i = 0; i < nameList.size(); i++)
            {
                org.setName(((Attribute)nameList.get(i)).getValue());
                org.setUrl(((Attribute)nameList.get(i)).getValue());
                org.setComment(((Attribute)commentList.get(i)).getValue());
                org.setType(((Attribute)typeList.get(i)).getValue().charAt(0));
                org = organize.createOrganize(org);
                logger.info("\u521B\u5EFA\u516C\u53F8\u6210\u529F!");
                org.setParentId(org.getId());
                org = (Organize)hbpservice.store(org);
                logger.info("\u521D\u59CB\u5316\u603B\u516C\u53F8: [" + ((Attribute)nameList.get(i)).getValue() + "] \u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u7EC4\u7EC7\u7ED3\u6784\u9519\u8BEF!");
        }
        return org;
    }

    private void initCourseSort()
        throws BusinessException
    {
        CourseSort cs = new CourseSort();
        cs.setName("\u672A\u5206\u7C7B");
        try
        {
            hbpservice.store(cs);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u8BFE\u7A0B\u5206\u7C7B\u9519\u8BEF!");
        }
    }

    public void initQuestion(String path)
        throws BusinessException
    {
        List sortList = null;
        List hardList = null;
        try
        {
            Document document = saxReader.read(new File(path));
            sortList = document.selectNodes("/questions/sort/@name");
            hardList = document.selectNodes("/questions/hard/@name");
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u89E3\u6790question.xml\u6587\u4EF6\u9519\u8BEF:" + path);
        }
        try
        {
            for(int i = 0; i < sortList.size(); i++)
            {
                QuestionSort sort = new QuestionSort();
                sort.setName(((Attribute)sortList.get(i)).getValue());
                hbpservice.store(sort);
                logger.info("\u521D\u59CB\u5316\u8BD5\u9898\u7C7B\u578B: [" + sort.getName() + "] \u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u8BD5\u9898\u5206\u7C7B\u5931\u8D25!");
        }
        try
        {
            for(int j = 0; j < hardList.size(); j++)
            {
                QuestionHard hard = new QuestionHard();
                hard.setName(((Attribute)hardList.get(j)).getValue());
                hbpservice.store(hard);
                logger.info("\u521D\u59CB\u5316\u8BD5\u9898\u96BE\u5EA6: [" + hard.getName() + " ]\u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u8BD5\u9898\u96BE\u6613\u5EA6\u5931\u8D25!");
        }
    }

    public void initAddress(String path)
        throws BusinessException
    {
        List sortList = null;
        try
        {
            Document document = saxReader.read(new File(path));
            sortList = document.selectNodes("/address/sort/@name");
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u89E3\u6790address.xml\u6587\u4EF6\u9519\u8BEF:" + path);
        }
        try
        {
            for(int i = 0; i < sortList.size(); i++)
            {
                AddressSort sort = new AddressSort();
                sort.setName(((Attribute)sortList.get(i)).getValue());
                hbpservice.store(sort);
                logger.info("\u521D\u59CB\u5316\u8054\u7CFB\u4EBA\u7C7B\u578B: [" + sort.getName() + "] \u6210\u529F!");
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521D\u59CB\u5316\u8BD5\u9898\u5206\u7C7B\u5931\u8D25!");
        }
    }

    private static Logger logger;
    private IOrganizeService organize;
    private IGroupService group;
    private IRoleService role;
    private IPrivilegeService privilege;
    public SAXReader saxReader;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.init.InitServiceImp.class.getName());
    }
}
